//@include "lib.jsx"

//初始化
function initUserSettings() {
    var defaultSettings = {
        isSavePSD: false,
        isSaveJPG: false,
        isSavePNG: false,
        isSaveJPGW: false,
        isSavePNGW: false,
        isSaveOwnFolder: false,
        isCloseDocument: false,
        isUseCurrDocName: false,
        isSaveDateFolder: false,
        isExecute: false,
        savePath: getOSDefaultPath(),
        saveName: getCurrentTime(),
        watermarkSel:2,
    };
    return defaultSettings;
}

// 更新函数
function updateSetting(key, value) {
    var userSettings = loadDataFromFile();
    userSettings[key] = value
    saveDataToFile(userSettings);
    //alert(userSettings[key] = value)
}

// 保存函数
function saveDataToFile(data) {
    var scriptFile = new File($.fileName);
    var filePath = scriptFile.path + getOsSolidus() + "userSettings.json";
    var jsonData = JSON.stringify(data);
    var dataFile = new File(filePath);
    // 打开文件以便写入数据
    if (dataFile.open("w")) {
        // 写入 JSON 格式的数据
        dataFile.write(jsonData);

        // 关闭文件
        dataFile.close();
        // alert("数据已保存到文件：" + filePath);
    } else {
        alert("无法打开文件以保存数据");
    }
}
//读取函数
function loadDataFromFile() {
    var scriptFile = new File($.fileName);
    var dataFilePath = scriptFile.path + getOsSolidus() + "userSettings.json";
    var dataFile = new File(dataFilePath);
    var data
    if (dataFile.exists) {
        if (dataFile.open("r")) {
            var content = dataFile.read();
            dataFile.close();
            try {
                data = JSON.parse(content);
            } catch (e) {
                //$.writeln("解析文件时出现错误: " + e);
                saveDataToFile(initUserSettings())
                data = loadDataFromFile()
            }

            //$.writeln(data.isSavePSD)
        } else(
            alert("写入失败")
        )
    }
    if (data.isSavePSD == undefined) {
        //$.writeln(initUserSettings())
        saveDataToFile(initUserSettings())
        return initUserSettings()
    } else {
        $.writeln(data);
        return data;
    }
}

// 保存整数值到文件
function saveIntToFile(intValue) {
    var scriptFile = new File($.fileName);
    var filePath = scriptFile.path + getOsSolidus() + "userIntValue.txt";
    var dataFile = new File(filePath);
    if (dataFile.open("w")) {
        dataFile.write(intValue.toString());
        dataFile.close();
    } else {
        alert("无法打开文件以保存数据");
    }
}

// 从文件中读取整数值
function loadIntFromFile() {
    var scriptFile = new File($.fileName);
    var filePath = scriptFile.path + getOsSolidus() + "userIntValue.txt";
    var dataFile = new File(filePath);
    var intValue;
    if (dataFile.exists) {
        if (dataFile.open("r")) {
            var content = dataFile.read();
            dataFile.close();
            intValue = parseInt(content, 10);
            if (isNaN(intValue)) {
                alert("文件内容无法解析为整数");
                intValue = null;
            }
        } else {
            alert("读取失败");
            intValue = null;
        }
    } else {
        alert("文件不存在");
        intValue = null;
    }
    return intValue;
}


// var userSettings = loadDataFromFile();
// saveDataToFile(userSettings)
// $.writeln(userSettings["savePath"])
// $.writeln(userSettings["saveName"])